package com.wd.car

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.gyf.immersionbar.ImmersionBar
import com.wd.buy.BuyFragment
import com.wd.car.databinding.ActivityMainBinding
import com.wd.common.base.BaseActivity
import com.wd.home.HomeFragment
import com.wd.lookcar.LookCarFragment
import com.wd.mine.MineFragment
import com.wd.rim.RimFragment


class MainActivity : BaseActivity<ActivityMainBinding>(){
    override fun getViewBinding()=ActivityMainBinding.inflate(layoutInflater)

    override fun initData() {
        ImmersionBar.with(this)
//            .titleBarMarginTop(binding.homePager)   状态栏可以下调
            .statusBarAlpha(0.4f).init()
        binding.homePager.apply {
            adapter=ScreenSlidePagerAdapter(supportFragmentManager,lifecycle)
            isUserInputEnabled=false
        }
        binding.radioGroup.apply {
            check(R.id.tab_one)
            setOnCheckedChangeListener { group, checkedId ->
                when (checkedId) {
                    R.id.tab_one ->
                        binding.homePager.setCurrentItem(0,false)//取消点击底部导航按钮时，viewPager2的滑动动画效果

                    R.id.tab_two ->
                        binding.homePager.setCurrentItem(1,false)

                    R.id.tab_three ->
                        binding.homePager.setCurrentItem(2,false)

                    R.id.tab_four ->
                        binding.homePager.setCurrentItem(3,false)

                    R.id.tab_five ->
                        binding.homePager.setCurrentItem(4,false)

                }
            }
        }


    }

    private inner class ScreenSlidePagerAdapter(fa: FragmentManager, lc: Lifecycle) :
        FragmentStateAdapter(fa, lc) {
        val list = mutableListOf(
            HomeFragment(),
            RimFragment(),
            LookCarFragment(),
            BuyFragment(),
            MineFragment()
        )
        override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
            super.onAttachedToRecyclerView(recyclerView)
            // 设置recyclerView itemCacheSize
            recyclerView.setItemViewCacheSize(itemCount)
        }


        override fun getItemCount(): Int = list.size

        override fun createFragment(position: Int): Fragment {
            return list[position]
        }
    }

}